package com.keehin;

import java.math.BigDecimal;
import java.util.Date;
import java.awt.Color;

import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

public class MyRenderer extends DefaultTableCellRenderer {

	private static final long serialVersionUID = 1L;

	public MyRenderer() {
		super();
	}

	protected void setValue(Object value) {
		if (value != null) {

			this.setForeground(Color.BLACK);
			if (value instanceof Date) {
				this.setText(Database.dateFormat.format(value));
				this.setHorizontalAlignment(SwingConstants.RIGHT);
			} else if (value instanceof BigDecimal) {
				this.setHorizontalAlignment(SwingConstants.RIGHT);
				float val = ((BigDecimal) value).floatValue();
				this.setText(String.format("%,.2f", val));
				if (val < 0)
					this.setForeground(Color.red);
			} else if (value instanceof Float) {
				this.setHorizontalAlignment(SwingConstants.RIGHT);
				this.setText(String.format("%,.2f", value));
			} else if (value instanceof Integer) {
				this.setHorizontalAlignment(SwingConstants.RIGHT);
				this.setText(String.format("%,d", value));
				if (((Integer) value).intValue() < 0)
					this.setForeground(Color.red);
			} else {
				this.setHorizontalAlignment(SwingConstants.LEFT);
				this.setText(value.toString());
			}
		} else
			this.setText(null);
	}
}
